package com.gitee.pifeng.monitoring.ui.business.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.pifeng.monitoring.common.constant.ZeroOrOneConstants;
import com.gitee.pifeng.monitoring.ui.business.web.dao.IXjJtopoDao;
import com.gitee.pifeng.monitoring.ui.business.web.entity.MonitorDb;
import com.gitee.pifeng.monitoring.ui.business.web.entity.XjDataLink;
import com.gitee.pifeng.monitoring.ui.business.web.service.IXjJTopoService;
import com.gitee.pifeng.monitoring.ui.business.web.vo.LayUiAdminResultVo;
import com.gitee.pifeng.monitoring.ui.business.web.vo.MonitorDbVo;
import com.gitee.pifeng.monitoring.ui.business.web.vo.XjDataLinkDetailVo;
import com.gitee.pifeng.monitoring.ui.business.web.vo.XjDataLinkVo;
import com.gitee.pifeng.monitoring.ui.constant.WebResponseConstants;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 数据库表服务实现类
 * </p>
 *
 * @author 皮锋
 * @custom.date 2020-12-19
 */
@Service
public class XjJtopoServiceImpl extends ServiceImpl<IXjJtopoDao, XjDataLink> implements IXjJTopoService {

    /**
     * 数据库表数据访问对象
     */
    @Autowired
    private IXjJtopoDao xjJtopoDao;


    @Override
    public LayUiAdminResultVo addDataLink(ArrayList<XjDataLink> xjDataLinks) {
        Integer resCount = 0;
        // 根据数链名称，查询数据库中是否已经存在此数链名称的记录
        LambdaQueryWrapper<XjDataLink> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(XjDataLink::getDataLinkName, xjDataLinks.get(0).getDataLinkName());
        Integer count = this.xjJtopoDao.selectCount(lambdaQueryWrapper);
        // 已经存在
        if (count != null && count > 0) {
            System.out.println("=============== 该数链名称已存在 ===============");
            return LayUiAdminResultVo.ok(WebResponseConstants.EXIST);
        }
        for (int i = 0; i < xjDataLinks.size(); i++) {
            xjDataLinks.get(i).setCreateTime(new Date());
            int result = this.xjJtopoDao.insert(xjDataLinks.get(i));
            System.out.println("result:" + result);
            if(result > 0){
                resCount = resCount + result;
            }
        }
        System.out.println("resCount:" + resCount);
        if (resCount == xjDataLinks.size()) {
            return LayUiAdminResultVo.ok(WebResponseConstants.SUCCESS);
        }
        return LayUiAdminResultVo.ok(WebResponseConstants.FAIL);
    }

    @Override
    public Page<XjDataLinkVo> getDataLinkList(Long current, Long size, String dataLinkName, String remark) {
        // 查询数据库
        IPage<XjDataLink> ipage = new Page<>(current, size);
        LambdaQueryWrapper<XjDataLink> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 只查询部分字段
        lambdaQueryWrapper.select(XjDataLink::getDataLinkName, XjDataLink::getCreateTime, XjDataLink::getRemark);
        if (StringUtils.isNotBlank(dataLinkName)) {
            lambdaQueryWrapper.like(XjDataLink::getDataLinkName, dataLinkName);
        }
        if (StringUtils.isNotBlank(remark)) {
            lambdaQueryWrapper.like(XjDataLink::getRemark, remark);
        }
        IPage<XjDataLink> xjDataLinkPage = this.xjJtopoDao.selectPage(ipage, lambdaQueryWrapper);
        List<XjDataLink> xjDataLinks = xjDataLinkPage.getRecords();
        // 转换成数据库信息表现层对象
        List<XjDataLinkVo> xjDataLinkVos = Lists.newLinkedList();
        for (XjDataLink xjDataLink : xjDataLinks) {
            boolean exists = xjDataLinkVos.stream().anyMatch(o -> o.getDataLinkName().equals(xjDataLink.getDataLinkName()));
            System.out.println("exists：" + !exists);
            if(!exists){
                XjDataLinkVo xjDataLinkVo = new XjDataLinkVo();
                xjDataLinkVo.setDataLinkName(xjDataLink.getDataLinkName());
                xjDataLinkVo.setCreateTime(xjDataLink.getCreateTime());
                xjDataLinkVo.setRemark(xjDataLink.getRemark());
                xjDataLinkVos.add(xjDataLinkVo);
            }
        }
        // 设置返回对象
        Page<XjDataLinkVo> xjDataLinkVoPage = new Page<>();
        xjDataLinkVoPage.setRecords(xjDataLinkVos);
        xjDataLinkVoPage.setTotal(xjDataLinkVos.size());
        return xjDataLinkVoPage;
    }

    @Override
    public LayUiAdminResultVo getDataLinkDetail(String dataLinkName) {

        List<XjDataLinkDetailVo> xjDataLinks = xjJtopoDao.getDataLinkList(dataLinkName);
        System.out.println("xjDataLinks:" + xjDataLinks.size());
        System.out.println("xjDataLinks.toString:" + xjDataLinks.toString());

        ArrayList<XjDataLinkDetailVo> data = new ArrayList<>();
        for (XjDataLinkDetailVo xjDataLink : xjDataLinks) {
            if(xjDataLink.getNodeX() != null){
                if(xjDataLink.getIsOnline().equals("0")){
                    xjDataLink.setIsOnline("离线");
                }else{
                    xjDataLink.setIsOnline("在线");
                }
                String text = "";
                text += "连接名称：" + xjDataLink.getConnName() + "<br>";
                text += "用户名：" + xjDataLink.getUsername() + "<br>";
                text += "类型：" + xjDataLink.getDbType() + "<br>";
                text += "url：" + xjDataLink.getUrl() + "<br>";
                text += "分组：" + xjDataLink.getMonitorGroup() + "<br>";
                text += "环境：" + xjDataLink.getMonitorEnv() + "<br>";
                text += "状态：" + xjDataLink.getIsOnline() + "<br>";
                text += "描述：" + xjDataLink.getDbDesc();
                xjDataLink.setText(text);
                data.add(xjDataLink);
            }
        }

        for (XjDataLinkDetailVo xjDataLink : data) {
            ArrayList<XjDataLinkDetailVo> links = new ArrayList<>();
            for (XjDataLinkDetailVo link : xjDataLinks) {
                if(link.getFromNode().equals(xjDataLink.getFromNode()) && link.getNodeX() == null){
                    System.out.println("添加一条：" + link);
                    links.add(link);
                }
            }
            xjDataLink.setLinks(links);
        }

        return LayUiAdminResultVo.ok(data);
    }

    @Override
    public LayUiAdminResultVo deleteDataLink(List<XjDataLink> xjDataLinks) {
        for (int i = 0; i < xjDataLinks.size(); i++) {
            System.out.println("dataLinkName:" + xjDataLinks.get(i).getDataLinkName());
            // 查询数据库
            LambdaQueryWrapper<XjDataLink> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            // 只查询部分字段
            lambdaQueryWrapper.select(XjDataLink::getLdId);
            lambdaQueryWrapper.eq(XjDataLink::getDataLinkName, xjDataLinks.get(i).getDataLinkName());

            List<XjDataLink> deleteXjDataLinks = this.xjJtopoDao.selectList(lambdaQueryWrapper);
            System.out.println("deleteXjDataLinks:" + deleteXjDataLinks.size());
            List<Long> ids = Lists.newArrayList();
            for (XjDataLink xjDataLink : deleteXjDataLinks) {
                System.out.println("getLdId:" + xjDataLink.getLdId());
                ids.add(xjDataLink.getLdId());
            }
            this.xjJtopoDao.deleteBatchIds(ids);
        }

        return LayUiAdminResultVo.ok(WebResponseConstants.SUCCESS);
    }


}
